package net.minhdiep.androiddialoglistener;


import android.location.Location;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.TextView;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient;
import com.google.android.gms.location.LocationClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;


/*
 * User: Minh Diep
 * Date: 6/6/13
 * Time: 5:10 PM
 */

public class LocationServiceActivity extends FragmentActivity implements
        GooglePlayServicesClient.ConnectionCallbacks,
        GooglePlayServicesClient.OnConnectionFailedListener, LocationListener {

    // Milliseconds per second
    private static final int MILLISECONDS_PER_SECOND = 1000;
    // Update frequency in seconds
    public static final int UPDATE_INTERVAL_IN_SECONDS = 5;
    // Update frequency in milliseconds
    private static final long UPDATE_INTERVAL =
            MILLISECONDS_PER_SECOND * UPDATE_INTERVAL_IN_SECONDS;
    // The fastest update frequency, in seconds
    private static final int FASTEST_INTERVAL_IN_SECONDS = 1;
    // A fast frequency ceiling in milliseconds
    private static final long FASTEST_INTERVAL =
            MILLISECONDS_PER_SECOND * FASTEST_INTERVAL_IN_SECONDS;


    private LocationClient locationClient;
    private TextView statusTextView, latTextView, lngTextView;
    private LocationRequest mLocationRequest;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_location_service);
        statusTextView = (TextView) findViewById(R.id.location_service_status_tv);
        latTextView = (TextView) findViewById(R.id.location_service_lat_tv);
        lngTextView = (TextView) findViewById(R.id.location_service_lng_tv);
        locationClient = new LocationClient(this, this, this);

        // Create the LocationRequest object
        mLocationRequest = LocationRequest.create();
        // Use high accuracy
        mLocationRequest.setPriority(
                LocationRequest.PRIORITY_HIGH_ACCURACY);
        // Set the update interval to 5 seconds
        mLocationRequest.setInterval(UPDATE_INTERVAL);
        // Set the fastest update interval to 1 second
        mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
    }

    @Override
    protected void onStart() {
        super.onStart();
        locationClient.connect();
    }

    @Override
    protected void onStop() {
        if (locationClient.isConnected()) {
            locationClient.removeLocationUpdates(this);
        }
        locationClient.disconnect();
        super.onStop();
    }

    @Override
    public void onConnected(Bundle bundle) {
        statusTextView.setText("Status: Connected!");
        locationClient.requestLocationUpdates(mLocationRequest, this);
        Location currentLocation = locationClient.getLastLocation();
        if (currentLocation != null) {
            latTextView.setText("Lat: " + currentLocation.getLatitude());
            lngTextView.setText("Lng: " + currentLocation.getLongitude());
        }
    }

    @Override
    public void onDisconnected() {
        statusTextView.setText("Status: Disconnected!");
    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {

    }

    @Override
    public void onLocationChanged(Location location) {
        latTextView.setText("Lat: " + location.getLatitude());
        lngTextView.setText("Lng: " + location.getLongitude());
    }

}